"""
2024. 考试的最大困扰度
https://leetcode.cn/problems/maximize-the-confusion-of-an-exam/
同解法：
1004. 最大连续1的个数 III  调用 ConsecType 1 次
424. 替换后的最长重复字符  调用 ConsecType |E| 次
"""
class Solution:
    def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int:
        n = len(answerKey)
        
        # 不知道换成 T or F？分两次调用就行了！取 T, F 最大值！
        def ConsecType(ch: str) -> int:
            max_len = 0
            l, r = 0, 0
            curr = 0
            while r < n:
                curr += answerKey[r] != ch
                while curr > k:
                    curr -= answerKey[l] != ch
                    l += 1
                max_len = max(max_len, r-l+1)
                r += 1
            return max_len
        
        return max(ConsecType('T'), ConsecType('F'))
